<template>
  <el-card>
    <el-table style="margin: 10px 0;" border :data="skuArr">
      <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column label="名字" show-overflow-tooltip prop="skuName"></el-table-column>
      <el-table-column label="描述" show-overflow-tooltip prop="skuDesc"></el-table-column>
      <el-table-column label="图片" >
        <template #="{row}">
          <img :src="row.skuDefaultImg" alt="" style="width: 50px;height: 50px;">
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight"></el-table-column>
      <el-table-column label="价格" prop="price"></el-table-column>
      <el-table-column label="操作" width="250px" fixed="right">
        <template #="{row}">
          <el-button @click="updateSale(row)" type="success" size="small" :icon="row.isSale==1?'Bottom':'Top'"></el-button>
          <el-button @click="updateSku" type="primary" size="small" icon="Edit"></el-button>
          <el-button @click="findSku(row)" type="info" size="small" icon="InfoFilled"></el-button>
          <el-popconfirm :title="`确认删除${row.skuName}吗？`" width="200" @confirm="deleteSku(row.id)">
            <template #reference>
              <el-button type="danger" size="small" icon="Delete" title="删除SKU"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getHasSku"
      @size-change="handler"
    />
    <!-- 抽屉组件 商品详情 -->
    <el-drawer v-model="drawer">
      <template #header><h4>查看商品详情</h4></template>
      <template #default>
        <el-row style="margin: 10px 0;">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{skuInfo.skuName}}</el-col>
        </el-row>
        <el-row style="margin: 10px 0;">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{skuInfo.skuDesc}}</el-col>
        </el-row>
        <el-row style="margin: 10px 0;">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{skuInfo.price}}</el-col>
        </el-row>
        <el-row style="margin: 10px 0;">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag v-for="item in skuInfo.skuAttrValueList" :key="item.id" style="margin: 5px;">{{item.valueName}}</el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0;">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id" style="margin: 5px;">{{item.saleAttrName}}</el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0;">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="" style="width: 100%;height: 100%;">
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import {reqSkuList,reqSaleSku,reqCancelSale,reqSkuInfo,reqRemoveSku} from '@/api/product/sku/index'
import type {SkuResponseData,SkuData,SkuInfoData} from '@/api/product/sku/type'
import { ElMessage } from 'element-plus';
let pageNo = ref<number>(1)
let pageSize = ref<number>(10)
let total = ref<number>(0)
let skuArr = ref<SkuData[]>([])
let drawer = ref<boolean>(false)
let skuInfo = ref<any>({})
// 组件挂载完毕
onMounted(() => {
  getHasSku()
})
const getHasSku = async(pager=1)=> {
  // 当前分页器的页码
  pageNo.value = pager
  let res:SkuResponseData = await reqSkuList(pageNo.value,pageSize.value)
  if(res.code==200){
    total.value = res.data.total
    skuArr.value = res.data.records
  }
}
// 分页器下拉菜单发生变化时触发
const handler = ()=> {
  getHasSku()
}
// 商品上架与下架
const updateSale = async(row:SkuData)=>{
  // 当前商品isSale==1,上架状态，接口请求更新为下架状态
  if(row.isSale==1){
    await reqCancelSale((row.id as number))
    ElMessage.success('下架成功！')
    getHasSku(pageNo.value)
  }else{
    await reqSaleSku((row.id as number))
    ElMessage.success('上架成功！')
    getHasSku(pageNo.value)
  }
}
const updateSku = ()=>{
  ElMessage.success('修改中...')
}
const findSku = async(row:SkuData)=> {
  drawer.value = true
  let res:SkuInfoData = await reqSkuInfo((row.id as number))
  skuInfo.value = res.data  
}
const deleteSku = async(id:number)=> {
  let res:any = await reqRemoveSku(id)
  if(res.code==200){
    ElMessage.success('删除成功！')
    getHasSku(skuArr.value.length>1?pageNo.value:pageNo.value-1)
  }else{
    ElMessage.error('删除失败！系统数据，不能删除！')
  }
}
</script>

<style scoped lang="scss">
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>